package ioana.pack;

import java.io.IOException;
import java.io.StringBufferInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

class WeatherThread extends Thread 
{
	private String url;
	private Prognoza context;
	
	public WeatherThread (String url, Prognoza context)
	{
		this.url = url;
		this.context = context;
	}
	
    public void run()
    {
    	
		 HttpGet getMethod=new HttpGet(url);
	     HttpClient client = new DefaultHttpClient ();
	        
	    	try 
	    	{


	    		ResponseHandler<String> responseHandler=new BasicResponseHandler();
	    		final String responseBody=client.execute(getMethod, responseHandler);
	    		
	    		context.runOnUiThread(new Runnable() 
	    		{
					public void run() 
				{
					context.showWeather(responseBody);
				}
				});
	    	}
	    	catch (final IOException ex)
	    	{
	    		
	    		context.runOnUiThread(new Runnable() {
					
					public void run() {
						// TODO Auto-generated method stub
						ex.printStackTrace();
						context.showWeather("");
					}
				});
	    	}	
    }
}

public class Prognoza extends Activity
{
	String oras;
	
	public void putImage(String prog,ImageView imag1)
	{
        if(prog.equalsIgnoreCase("/ig/images/weather/mostly_sunny.gif")) 
        	{
        	imag1.setImageResource(R.drawable.mostly_sunny);
        	}
	    else if(prog.equalsIgnoreCase("/ig/images/weather/chance_of_snow.gif"))
	    {
			imag1.setImageResource(R.drawable.chance_of_snow);	
	    }
		else if (prog.equalsIgnoreCase("/ig/images/weather/chance_of_storm.gif"))
		{
			imag1.setImageResource(R.drawable.chance_of_storm);
		}
		else if (prog.equalsIgnoreCase("/ig/images/weather/chance_of_tstorm.gif"))
		{
			imag1.setImageResource(R.drawable.chance_of_tstorm);
		}
		else if (prog.equalsIgnoreCase("/ig/images/weather/cloudy.gif"))
		{
			imag1.setImageResource(R.drawable.cloudy);
		}
		else if(prog.equalsIgnoreCase("/ig/images/weather/Dust.gif"))
		{
			imag1.setImageResource(R.drawable.dust);
		}
		else if (prog.equalsIgnoreCase("/ig/images/weather/Flurries.gif"))
		{
			imag1.setImageResource(R.drawable.flurries);
		}
		else if (prog.equals("/ig/images/weather/Fog.gif"))
		{
			imag1.setImageResource(R.drawable.fog);
		}
		else if(prog.equalsIgnoreCase("/ig/images/weather/Haze.gif"))
		{
			imag1.setImageResource(R.drawable.haze);
		}
		else if (prog.equalsIgnoreCase("/ig/images/weather/Icy.gif"))
		{
			imag1.setImageResource(R.drawable.icy);
		}
		else if(prog.equalsIgnoreCase("/ig/images/weather/Mist.gif"))
		{
			imag1.setImageResource(R.drawable.mist);
		}
		else if (prog.equalsIgnoreCase("/ig/images/weather/Mostly_Cloudy.gif"))
		{
			imag1.setImageResource(R.drawable.mostly_cloudy);
		}
		else if (prog.equalsIgnoreCase("/ig/images/weather/Mostly_Sunny.gif"))
		{
			imag1.setImageResource(R.drawable.mostly_sunny);
		}
		else if (prog.equalsIgnoreCase("/ig/images/weather/Partly_Cloudy.gif"))
		{
			imag1.setImageResource(R.drawable.partly_cloudy);
		}
		else if (prog.equalsIgnoreCase("/ig/images/weather/Rain.gif"))
		{
			imag1.setImageResource(R.drawable.rain);
		}
		else if (prog.equalsIgnoreCase("/ig/images/weather/Sleet.gif"))
		{
			imag1.setImageResource(R.drawable.sleet);
		}
		else if (prog.equalsIgnoreCase("/ig/images/weather/Smoke.gif"))
		{
			imag1.setImageResource(R.drawable.smoke);
		}
		else if (prog.equalsIgnoreCase("/ig/images/weather/Snow.gif"))
		{
			imag1.setImageResource(R.drawable.snow);
		}
		else if (prog.equalsIgnoreCase("/ig/images/weather/Storm.gif"))
		{
			imag1.setImageResource(R.drawable.storm);
		}
		else if (prog.equalsIgnoreCase("/ig/images/weather/Sunny.gif"))
		{
			imag1.setImageResource(R.drawable.sunny);
		}
		else if(prog.equalsIgnoreCase("/ig/images/weather/thunderstorm.gif")) 
			{
			imag1.setImageResource(R.drawable.thunderstorm);
			}
		else if(prog.equalsIgnoreCase("/ig/images/weather/chance_of_rain.gif")) 
			{
			imag1.setImageResource(R.drawable.chance_of_rain);
			}
	}
	public void getDate(Node n,String tag, TextView parametru)
	{
		Element d = (Element)((Element)n).getElementsByTagName(tag).item(0);
		parametru.setText(d.getAttribute("data"));
	}
	public String condition (Node n, String tag)
	{
		Element d = (Element)((Element)n).getElementsByTagName(tag).item(0);
		return d.getAttribute("data");
	}
	
	TextView date,condition,temp,humidity,wind,day1,low1,condition1,day2,low2,condition2,day3,low3,condition3;
	ImageView icon, icon1,icon2,icon3;
	ProgressDialog progress;
	
	@Override
    public void onStart ()
    {
    	super.onStart();
    	Intent i=getIntent();
		 String o=i.getStringExtra("nume oras");
		 o=o.replace(' ', '+');
		 o=o.replace('-','+');
		 WeatherThread weatherThread = new WeatherThread("http://www.google.com/ig/api?weather="+o, this);
		 progress = new ProgressDialog(this);
		 progress.setTitle("Se incarca");
		 progress.setMessage("Va rugam sa asteptati");
		 progress.show();
		 weatherThread.start();
    	
    }
	
	 public void onCreate(Bundle savedInstanceState) 
	    {
		 super.onCreate(savedInstanceState);
		 setContentView(R.layout.meteo);   
	        	    
	    }
		 
	 public void showWeather (String responseBody)
	 {
		 try
		 {
 		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
     	DocumentBuilder db = dbf.newDocumentBuilder();
     	Document doc = db.parse(new StringBufferInputStream(responseBody));
     	
     	date=(TextView)findViewById(R.id.data);
     	condition=(TextView) findViewById(R.id.condition);
     	temp=(TextView)findViewById(R.id.temp);
     	humidity=(TextView)findViewById(R.id.humidity);
     	wind=(TextView)findViewById(R.id.wind);
     	day1=(TextView)findViewById(R.id.day1);
        low1=(TextView)findViewById(R.id.low1);
        condition1=(TextView)findViewById(R.id.condition1);
        day2=(TextView)findViewById(R.id.day2);
        low2=(TextView)findViewById(R.id.low2);
        condition2=(TextView)findViewById(R.id.condition2);
        day3=(TextView)findViewById(R.id.day3);
        low3=(TextView)findViewById(R.id.low3);
        condition3=(TextView)findViewById(R.id.condition3);
        icon=(ImageView)findViewById(R.id.icon);
        icon1=(ImageView)findViewById(R.id.icon1);
        icon2=(ImageView)findViewById(R.id.icon2);
        icon3=(ImageView)findViewById(R.id.icon3);
        
        
     	NodeList node = doc.getElementsByTagName("forecast_information");
     	if (node.getLength()>0)
     	{
     		Node m = node.item(0);
     		Element y = (Element)((Element)m).getElementsByTagName("forecast_date").item(0);
     		System.out.println("se scrie data");
     		date.setText("Today  "+y.getAttribute("data"));
     		
     	
     	NodeList nodeList = doc.getElementsByTagName("current_conditions");
     	if (nodeList.getLength()>0)
     	{
     		
     		Node n = nodeList.item(0);
     		getDate(n,"condition",condition);
     		Element d = (Element)((Element)n).getElementsByTagName("temp_c").item(0);
     		temp.setText(d.getAttribute("data")+" Celsius degrees");
     		getDate(n, "humidity", humidity);
     		getDate(n, "wind_condition", wind);
     		d = (Element)((Element)n).getElementsByTagName("icon").item(0);
     		String imagine=d.getAttribute("data");
     		putImage(imagine,icon);
     	}
     	nodeList = doc.getElementsByTagName("forecast_conditions");
     	if (nodeList.getLength()>0)
     	{
     		Node n = nodeList.item(0);
     		getDate(n, "day_of_week", day1);
     		Element d = (Element)((Element)n).getElementsByTagName("low").item(0);
     		String s=d.getAttribute("data").toString();
     		int x=Integer.parseInt(s);
     		x=(x-32)*5/9;
	        low1.setText(x+" Celsius degrees");
	        getDate(n, "condition", condition1);
	        d = (Element)((Element)n).getElementsByTagName("icon").item(0);
     		String imagine=d.getAttribute("data");
     		putImage(imagine,icon1);
	        	
	        n=nodeList.item(1);
	        getDate(n, "day_of_week", day2);
     		d = (Element)((Element)n).getElementsByTagName("low").item(0);
     		s=d.getAttribute("data").toString();
     	    x=Integer.parseInt(s);
     		x=(x-32)*5/9;
	        low2.setText(x+" Celsius degress");
	        getDate(n, "condition", condition2);
	        d = (Element)((Element)n).getElementsByTagName("icon").item(0);
     		imagine=d.getAttribute("data");
     		putImage(imagine,icon2);
	        	
	        n=nodeList.item(2);
	        getDate(n, "day_of_week", day3);
     		d = (Element)((Element)n).getElementsByTagName("low").item(0);
     		s=d.getAttribute("data").toString();
     	    x=Integer.parseInt(s);
     		x=(x-32)*5/9;
	        low3.setText(x+" Celsius degrees");
	        getDate(n, "condition", condition3);
	        d = (Element)((Element)n).getElementsByTagName("icon").item(0);
     		imagine=d.getAttribute("data");
     		putImage(imagine,icon3);
     		System.out.println("icon3");
	 }
    }
     	else
     	{
     		date.setText("Nu s-a gasit orasul.");
     	}
		 }
		 catch (Exception ex)
		 {
			 ex.printStackTrace();
			 
		 }
         progress.dismiss();
	 }
}

